#!/bin/sh
export ERRBOT_ROOT=/opt/errbot
export ERRBOT_USER=errbot

function setup_account
{
    echo -e "\e[93mUser/Group\e[0m"
    mkdir -p ${ERRBOT_ROOT}
    addgroup ${ERRBOT_USER}
    addgroup ${ERRBOT_USER} ${ERRBOT_USER}
    adduser -S -g ${ERRBOT_USER} -h ${ERRBOT_ROOT} -s /bin/false ${ERRBOT_USER}
    chown -R ${ERRBOT_USER}:${ERRBOT_USER} ${ERRBOT_ROOT}
    id ${ERRBOT_USER}
    ls -ld ${ERRBOT_ROOT}
}

function install_prod_packages
{
    echo -e "\e[93mPackage\e[0m"
    apk add --no-cache py3-virtualenv python3 git openssl rsync
}

function install_build_packages
{
    install_prod_packages
    apk add --no-cache gcc python3-dev openssl-dev libffi-dev musl-dev
    python3 -m ensurepip
    rm -r /usr/lib/python*/ensurepip
    test ! -e /usr/bin/pip && ln -s pip3 /usr/bin/pip
    test ! -e /usr/bin/python && ln -sf /usr/bin/python3 /usr/bin/python
    test -d /root/.cache && rm -r /root/.cache
}

function install_openssh
{
    echo -e "\e[93mCopy virtualenv to final image\e[0m"
    apk add openssh
    ssh-keygen -b 4096 -t ed25519 -f /etc/ssh/ssh_host_ed25519_key
    echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config

}

function setup_virtual_env
{
    echo -e "\e[93mVirtual Environment\e[0m"
    virtualenv -p python3 ${ERRBOT_ROOT}
    source ${ERRBOT_ROOT}/bin/activate
}

function install_plugin
{
    GIT_URL="$(echo ${1} | cut -d@ -f1)"
    GIT_BRANCH="$(echo ${1} | cut -d@ -f2)"
    PLUGIN_DIR="$(basename ${GIT_URL} | cut -d. -f1)"
    cd ${ERRBOT_ROOT}/plugins
    git clone $GIT_URL
    cd $PLUGIN_DIR
    git checkout $GIT_BRANCH
    pip install -r requirements.txt
}

function install_backend
{
    GIT_URL="$(echo ${1} | cut -d@ -f1)"
    GIT_BRANCH="$(echo ${1} | cut -d@ -f2)"
    BACKEND_DIR="$(basename ${GIT_URL} | cut -d. -f1)"
    cd ${ERRBOT_ROOT}/backends
    git clone $GIT_URL
    cd $BACKEND_DIR
    git checkout $GIT_BRANCH
    pip install -r requirements.txt
}

function initialise_errbot
{
    echo -e "\e[93mErrbot initialisation\e[0m"
    cd ${ERRBOT_ROOT}
    errbot --init
    rm -rf /opt/errbot/plugins/err-example
}

function install_errbot
{
    echo -e "\e[93mErrbot plugins installation\e[0m"
    pip3 install --upgrade pip setuptools
    # Use slackclient 1.3.1 until support for slack 2.0 has been added to slack backend.
    pip3 install errbot slackclient==1.3.1 sleekxmpp IRC
    initialise_errbot
    for dirname in plugins backends log
    do
        mkdir -p ${ERRBOT_ROOT}/${dirname}
    done
    for plugin in 'https://github.com/nzlosh/err-stackstorm.git'
    do
        install_plugin $plugin
    done

    for backend in  'https://github.com/nzlosh/err-backend-rocketchat.git@maint_nzlosh' \
                    'https://github.com/nzlosh/err-backend-gitter.git@maint_nzlosh' \
                    'https://github.com/errbotio/err-backend-discord' \
                    'https://github.com/errbotio/err-backend-mattermost' \
                    'https://github.com/errbotio/err-backend-slackv3'
    do
        install_backend $backend
    done
}

function cleanup
{
    test -f /root/docker_build_installer && rm /root/docker_build_installer
}

function stage1_build
{
    setup_account
    install_build_packages
    setup_virtual_env
    install_errbot
}

function stage2_build
{
    setup_account
    install_prod_packages
    install_openssh
}

if [ "$1" == "stage1" ]
then
    stage1_build
else
    stage2_build
    cleanup
fi
